/**
 * Created by Jone on 2016/7/15.
 */

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
    function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
        $stateProvider
            .state('app.project_create', {
                url: '/project/create',
                title: 'Single View',
                controller: 'ProjectController',
                templateUrl: helper.basepath('project/create.html'),
                resolve: helper.resolveFor( 'toaster')
            })
            .state('app.project_list_all', {
                url: '/project/list/all',
                controller: 'ProjectController',
                templateUrl: helper.basepath('project/list.html'),
                resolve: helper.resolveFor('toaster', 'ngDialog')
            })
            .state('app.project_list_created', {
                url: '/project/list/created',
                controller: 'ProjectController',
                templateUrl: helper.basepath('project/listCreated.html'),
                resolve: helper.resolveFor('toaster', 'ngDialog')
            })
            .state('app.project_list_participated', {
                url: '/project/list/participated',
                controller: 'ProjectController',
                templateUrl: helper.basepath('project/listParticipated.html'),
                resolve: helper.resolveFor('toaster', 'ngDialog')
            })
    }]);


App.controller('ProjectController', function ($scope, $rootScope, $cookieStore, $http, $location, $window, $state, toaster) {

    $scope.create = function() {
        $scope.project.userId = $cookieStore.get('user')._id;
        $http.post("/projects", $scope.project)
            .success(function (response) {
                if (response.code == 0) {
                    toaster.pop('success', '提示', '创建成功');
                }
            });
    }


    $scope.goCreate = function() {
        $state.go('app.project_create');
    }

    $scope.select = function(project) {
        console.log(JSON.stringify(project));
        $cookieStore.put("project", project);
        $state.go('app-api.interface_list_view');
    }

    $scope.list = function(type) {
        var params = {userId : $cookieStore.get('user')._id};
        params.type = type;
        $http.get("/projects", {params : params})
            .success(function (response) {
                $scope.projects = response.data;
            })
            .error(function(data) {

            });
    }
    //$scope.list();

});